<template>
  <div class="about" style="width: 1200px; margin: auto">
    <h4 style="text-align: left; font-weight: bold">基础信息</h4>
    <br />
    <el-form
      ref="ruleFormRef"
      style="max-width: 1300px; width: 1100px; margin: auto"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="生产单号" prop="proCode">
            <el-input v-model="ruleForm.proCode" />
            <el-checkbox
              v-model="systemNumber"
              @change="generateSystemNumber"
              value="Online activities"
              name="type"
              >系统编号</el-checkbox
            >
          </el-form-item>
          <el-form-item label="负责人" prop="proPeo">
            <el-input v-model="ruleForm.proPeo" />
          </el-form-item>
          <el-form-item label="生产人员" prop="proPeople">
            <el-input v-model="ruleForm.proPeople" />
          </el-form-item>
          <el-form-item label="生产日期" prop="proDateInfo">
            <el-date-picker
              v-model="ruleForm.proDateInfo"
              type="date"
              label="Pick a date"
              placeholder="生产日期"
              style="width: 100%"
              format="YYYY/MM/DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="单据日期" prop="proDate">
            <el-date-picker
              v-model="ruleForm.proDate"
              type="date"
              label="Pick a date"
              placeholder="单据日期"
              style="width: 100%"
              format="YYYY/MM/DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="proPhone">
            <el-input
              v-model.number="ruleForm.proPhone"
              maxlength="11"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="销售单号" prop="SalesOrderNumber">
            <el-input
              v-model.number="ruleForm.SalesOrderNumber"
              @click="dialogTableVisible = true"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="备注" prop="proDesc">
            <el-input
              v-model="ruleForm.proDesc"
              type="textarea"
              maxlength="200"
              show-word-limit
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-dialog v-model="dialogTableVisible" title="选择销售单号" width="800">
      <el-table
        ref="multipleTableRef"
        :data="SaleList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          property="SalesOrderNumber"
          label="销售单号"
        ></el-table-column>
        <el-table-column property="SalesDate" label="销售日期" />
        <el-table-column property="ClientName" label="客户名称" />
        <el-table-column property="LinkMan" label="联系人" />
        <el-table-column
          property="ContactInformation"
          label="联系方式"
        ></el-table-column>
        <el-table-column
          property="ContactNumber"
          label="联系电话"
        ></el-table-column>
        <el-table-column property="DestinationOfDelivery" label="收货地址" />
        <el-table-column label="操作">
          <template v-slot="slot">
            <el-button type="danger" @click="DeleteSale(slot.row.SaleId)"
              >删除</el-button
            >
            <el-button type="warning" @click="EditSale(slot.row.SaleId)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <br />
    <br />
    <p style="text-align: center">
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        添加
      </el-button>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </p>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import axios from "axios";
import { formatDate } from "../production(生产管理)/filter.js";

import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
//定义弹出框默认值
const dialogTableVisible = ref(false);
interface SaleRuleForm {
  SalesOrderNumber: string;
  CustomerName: string;
  SalesDate: string;
  ContactInformation: string;
  LinkMan: string;
  DestinationOfDelivery: string;
  ContactNumber: string;
  Remark: string;
  ClientId: string;
}
interface RuleForm {
  proCode: string;
  proDate: string;
  proPeo: string;
  proPhone: string;
  proPeople: string;
  proDesc: string;
  proDateInfo: string;
  SalesOrderNumber: string;
}

const systemNumber = ref(false);
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const SaleList = ref([]);
const ruleForm = reactive<RuleForm>({
  proCode: "",
  proDate: "",
  proPeo: "",
  proPhone: "",
  proPeople: "",
  proDesc: "",
  proDateInfo: "",
  SalesOrderNumber: "",
});

const generateSystemNumber = () => {
  if (systemNumber.value) {
    ruleForm.proCode = "CG-" + formatDate(new Date(), "yyyymdhs");
  } else {
    ruleForm.proCode = "";
  }
};

const rules = reactive<FormRules<RuleForm>>({
  proCode: [
    {
      required: true,
      message: "请选择生产单号",
      trigger: "change",
    },
    { min: 2, max: 20, message: "长度在2到20之间", trigger: "blur" },
  ],
  proDate: [
    {
      required: true,
      message: "请选择单据日期",
      trigger: "change",
    },
  ],
  proPeo: [
    {
      required: true,
      message: "请输入负责人",
      trigger: "change",
    },
  ],
  proPhone: [
    {
      type: "integer",
      required: true,
      message: "请输入联系电话且联系电话必须是整数",
      trigger: "change",
    },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "联系电话格式不正确",
    },
  ],
  proPeople: [
    {
      required: true,
      message: "请输入生产人员",
      trigger: "change",
    },
  ],
  proDesc: [
    {
      required: true,
      message: "请输入备注",
      trigger: "change",
    },
    { min: 10, max: 200, message: "长度在10到200之间", trigger: "blur" },
  ],
  proDateInfo: [
    {
      required: true,
      message: "请选择生产日期",
      trigger: "change",
    },
  ],
});

const pagination = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid, fields) => {
    if (valid) {
      axios.post("Produce/AddProduce", ruleForm).then((res) => {
        if (res.data > 0) {
          ElMessage.success("添加成功！");
          router.push("/UserList/ProduceShow");
        } else {
          ElMessage.error("添加失败！");
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const handleSelectionChange = (val) => {
  if (val.length > 1) {
    ElMessage.error("请选择且只选择一条销售单号");
    return;
  }
  ruleForm.SalesOrderNumber = val[0].SalesOrderNumber;
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
const SaleListref = () => {
  axios
    .get("Sale/GetSalesManagement", {
      params: {
        PageSize: pagination.pageSize,
        PageIndex: pagination.pageIndex,
      },
    })
    .then((res) => {
      SaleList.value = res.data.Data;
      pagination.total = res.data.TotalCount;
    });
};
const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
SaleListref();
</script>

<style>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.form-wrapper {
  background: #f9f9f9;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.form-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
}

.form {
  max-width: 100%;
}

.button-group {
  text-align: center;
  margin-top: 20px;
}

.button-group .el-button {
  margin: 0 10px;
  border-radius: 20px;
  padding: 10px 20px;
}
</style>


